package data;

import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import fileProcessing.ParsingException;

public class EmailEntry extends AbstractDataType {
	
	String fieldType = "Email";
	String email;
	
	public EmailEntry(RandomAccessFile raf) throws ParsingException{
		try {
			email = raf.readUTF();
		} catch (IOException e) {
			throw new ParsingException("Error: "+e.getMessage()+" when reading email from table's UTF file");
		}
	}
	
	public EmailEntry(String ssnVal) throws ParsingException{
		if(isValid(ssnVal)){
			email = ssnVal;
		}
		else
			throw new ParsingException("email not valid.");
	}

	public EmailEntry() {}
	
	@Override
	public int compareTo(Object arg0) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public boolean isValid(String s) throws ParsingException {
		Pattern pattern = Pattern.compile("\\S+@[\\w.-]+.[a-z]{2,4}");
		Matcher matcher = pattern.matcher(s);
		return matcher.matches();
	}

	@Override
	public String getType() {
		return fieldType;
	}

}
